package com.phkj.qdh.ui.dialog

import android.content.Context
import android.view.ViewParent
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hjq.shape.view.ShapeButton
import com.lxj.xpopup.core.CenterPopupView
import com.phkj.qdh.R
import com.phkj.qdh.bean.FailoverReduceType
import com.phkj.qdh.ui.adapter.FailoverTypeReduceAdapter
import com.phkj.qdh.ui.adapter.FailoverTypeReduceChildAdapter
import com.phkj.qdh.utils.LogUtil
import com.phkj.qdh.utils.MyItemDecoration
import com.phkj.qdh.utils.gone
import com.phkj.qdh.utils.invisible

class FileoverReduceTypeSelectDialog(context: Context, val list: ArrayList<FailoverReduceType>) :
    CenterPopupView(context) {
    private var title: String = ""
    private var content: String = ""
    private var eventType:Int? = null
    private val failoverTypeReduceAdapter by lazy { FailoverTypeReduceAdapter() }
    private val failoverTypeReduceChildAdapter by lazy { FailoverTypeReduceChildAdapter() }
    fun setTitle(s: String) {
        this.title = s
    }

    fun setContent(s: String) {
        this.content = s
    }

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_select_common
    }

    override fun onCreate() {
        super.onCreate()

        val tvTitle = findViewById<TextView>(R.id.tv_title)
        val tvContent = findViewById<TextView>(R.id.tv_content)
        val rv = findViewById<RecyclerView>(R.id.rv_item)
        rv.run {
            layoutManager = LinearLayoutManager(context)
            adapter = failoverTypeReduceAdapter
            addItemDecoration(MyItemDecoration(width = 5))
        }
        failoverTypeReduceAdapter.submitList(list)
        tvTitle.text = "折抵类型"
//        tvContent.text = content
        failoverTypeReduceAdapter.setOnItemClickListener { adapter, _, pos ->
            eventType = adapter.getItem(pos)?.eventType
            if (!adapter.getItem(pos)!!.child .isNullOrEmpty()) {
                rv.adapter = failoverTypeReduceChildAdapter
                failoverTypeReduceChildAdapter.submitList(adapter.getItem(pos)!!.child)
            } else {
                listener?.ok(adapter.getItem(pos)!!.id,adapter.getItem(pos)!!.parentId,adapter.getItem(pos)!!.name,eventType)
                dismiss()
            }
        }
        failoverTypeReduceChildAdapter.setOnItemClickListener { adapter, _, pos ->
            if (!adapter.getItem(pos)!!.child .isNullOrEmpty()) {
                failoverTypeReduceChildAdapter.submitList(adapter.getItem(pos)!!.child)
            } else {
                listener?.ok(adapter.getItem(pos)!!.id,adapter.getItem(pos)!!.parentId, adapter.getItem(pos)!!.name,eventType)
                dismiss()
            }
        }

        findViewById<ShapeButton>(R.id.btn_confirm).invisible()
    }

    private  var listener: FailoverReduceTypeListener? = null
    fun setListener(listener: FailoverReduceTypeListener) {
        this.listener = listener
    }

}

interface FailoverReduceTypeListener {
    fun ok(id: Int, parentId: Int,name: String,i: Int?)
}